package com.vmtest;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
//import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.vmtest.R;

public class frmficheros extends Activity {

	Handler_sqlite myDbHelper;
	//private SQLiteDatabase db;

	ViewFlipper vflipperME;
	ViewFlipper vflipperCS;
	ViewFlipper vflipperFA;

	TabHost tabs;

	final Context context = this;
	public static int cont = 0;

	@SuppressWarnings("unused")
	private static final int MNU_OPC1 = 1;
	@SuppressWarnings("unused")
	private static final int MNU_OPC2 = 2;
	@SuppressWarnings("unused")
	private static final int MNU_OPC3 = 3;
	@SuppressWarnings("unused")
	private static final int MNU_OPC4 = 4;

	/* Listview selected row */
	private int mSelectedRow = 0;
	/* Right arrow icon on each listview row */
	private ImageView mMoreIv = null;

	private static final int ID_ADD = 1;
	private static final int ID_ACCEPT = 2;
	private static final int ID_UPLOAD = 3;
	private static final int ID_PREVISIT = 4;
	private static final int ID_VIEW = 5;

	// Evento al usar el boton de retorno
	@Override
	public void onBackPressed() {

		// do something on back.
		// this.startActivity(new Intent(frmficheros.this,frmPpal.class));

		// custom dialog
		final Dialog dialog = new Dialog(context);
		dialog.setContentView(R.layout.dialogmenu);
		dialog.setTitle("Ficheros");

		// set the custom dialog components - text, image and button
		TextView text = (TextView) dialog.findViewById(R.id.text);
		text.setText("¿Desea salir del modulo?");

		// personalizacion de imagen
		// ImageView image = (ImageView) dialog.findViewById(R.id.image);
		// image.setImageResource(R.drawable.ic_launcher);

		Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
		Button dialogButton1 = (Button) dialog
				.findViewById(R.id.dialogButtonCANCEL);
		// if button is clicked, close the custom dialog
		dialogButton.setOnClickListener(new OnClickListener() {
			// @Override
			public void onClick(View v) {
				// dialog.dismiss();
				Intent intentppal = new Intent(frmficheros.this, frmPpal.class);
				startActivity(intentppal);
			}
		});

		dialogButton1.setOnClickListener(new OnClickListener() {
			// @Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		dialog.show();

		return;
	}

	public void ExitApp() {

		// do something on back.
		// this.startActivity(new Intent(frmficheros.this,frmPpal.class));

		// custom dialog
		final Dialog dialog = new Dialog(context);
		dialog.setContentView(R.layout.dialogmenu);
		dialog.setTitle("Ficheros");

		// set the custom dialog components - text, image and button
		TextView text = (TextView) dialog.findViewById(R.id.text);
		text.setText("¿Desea salir de la aplicacion?");

		// personalizacion de imagen
		// ImageView image = (ImageView) dialog.findViewById(R.id.image);
		// image.setImageResource(R.drawable.ic_launcher);

		Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
		Button dialogButton1 = (Button) dialog
				.findViewById(R.id.dialogButtonCANCEL);
		// if button is clicked, close the custom dialog
		dialogButton.setOnClickListener(new OnClickListener() {
			// @Override
			public void onClick(View v) {
				// dialog.dismiss();
				Intent intentlogin = new Intent(frmficheros.this,
						LoginActivity.class);
				startActivity(intentlogin);
				finishFromChild(getParent());
				moveTaskToBack(true);
			}
		});

		dialogButton1.setOnClickListener(new OnClickListener() {
			// @Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		dialog.show();

		return;
	}

	public void GuardarCambios() {

		// do something on back.
		// this.startActivity(new Intent(frmficheros.this,frmPpal.class));

		// custom dialog
		final Dialog dialog = new Dialog(context);
		dialog.setContentView(R.layout.dialogmenu);
		dialog.setTitle("Ficheros");

		// set the custom dialog components - text, image and button
		TextView text = (TextView) dialog.findViewById(R.id.text);
		text.setText("¿Desea guardar los cambios?");

		// personalizacion de imagen
		// ImageView image = (ImageView) dialog.findViewById(R.id.image);
		// image.setImageResource(R.drawable.ic_launcher);

		Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
		Button dialogButton1 = (Button) dialog
				.findViewById(R.id.dialogButtonCANCEL);
		// if button is clicked, close the custom dialog
		dialogButton.setOnClickListener(new OnClickListener() {
			// @Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		dialogButton1.setOnClickListener(new OnClickListener() {
			// @Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		dialog.show();

		vflipperME.setDisplayedChild(0);
		vflipperCS.setDisplayedChild(0);
		vflipperFA.setDisplayedChild(0);

		return;
	}

	/* Crear opciones del menu interno */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Alternativa 1

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_ficheros, menu);

		return true;
	}

	/* Eventos a realizar a seleccionar opcion de menu interno */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {

		// Fichero
		case R.id.MnuOpc1:

			if (tabs.getCurrentTab() == 0) {
				vflipperME.setDisplayedChild(0);
			} else if (tabs.getCurrentTab() == 1) {
				vflipperCS.setDisplayedChild(0);
			} else if (tabs.getCurrentTab() == 2) {
				vflipperFA.setDisplayedChild(0);
			}
			return true;
			// Nuevo Registro
		case R.id.MnuOpc2:

			if (tabs.getCurrentTab() == 0) {
				vflipperME.setDisplayedChild(1);
			} else if (tabs.getCurrentTab() == 1) {
				vflipperCS.setDisplayedChild(1);
			} else if (tabs.getCurrentTab() == 2) {
				vflipperFA.setDisplayedChild(1);
			}
			return true;
			// Solicitudes
		case R.id.MnuOpc3:

			if (tabs.getCurrentTab() == 0) {
				vflipperME.setDisplayedChild(2);
			} else if (tabs.getCurrentTab() == 1) {
				vflipperCS.setDisplayedChild(2);
			} else if (tabs.getCurrentTab() == 2) {
				vflipperFA.setDisplayedChild(2);
			}
			// Fichero Regional
		case R.id.MnuOpc4:

			if (tabs.getCurrentTab() == 0) {
				vflipperME.setDisplayedChild(3);
			} else if (tabs.getCurrentTab() == 1) {
				vflipperCS.setDisplayedChild(3);
			} else if (tabs.getCurrentTab() == 2) {
				vflipperFA.setDisplayedChild(3);
			}
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void cambiodatos() {

		return;
	}

	/* eventos a realizar al cargar formulario */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.frmficheros);

		myDbHelper = new Handler_sqlite (this);
		myDbHelper.getWritableDatabase();

		/*
		 * final EditText txtUserName = (EditText)findViewById(R.id.editText1);
		 * final EditText txtPassword = (EditText)findViewById(R.id.editText2);
		 */
		Button btnGuardarNRegFA = (Button) findViewById(R.id.buttonGuardarNRegFA);
		btnGuardarNRegFA.setOnClickListener(new OnClickListener() {

			// @Override
			public void onClick(View v) {
				final EditText txtNombreFA = (EditText) findViewById(R.id.editnombrefa);
				final EditText txtDireccionFA = (EditText) findViewById(R.id.editdireccionfa);
				String nombre = txtNombreFA.getText().toString();
				String direccion = txtDireccionFA.getText().toString();
				
				Handler_sqlite entrada = new Handler_sqlite(context);
							
				try {
					
					entrada.abrir();
					entrada.getWritableDatabase();
					entrada.insertarReg(nombre,direccion, direccion, direccion, direccion, direccion, direccion, direccion, direccion, direccion);
					entrada.close();	
					/*myDbHelper.openDataBase();
					myDbHelper.insertarNRegFA(nombre,direccion);
					myDbHelper.close();*/
					
					Toast.makeText(frmficheros.this, R.string.message, Toast.LENGTH_LONG).show();

				} catch (Exception e) {
					//Toast.makeText(frmficheros.this, e.getMessage(), Toast.LENGTH_LONG).show();
					throw new Error("Error insertando en dase de datos.");
				}
			}
		});

		/* viewflipper medicos */
		vflipperME = (ViewFlipper) findViewById(R.id.viewFlipperME);
		vflipperCS = (ViewFlipper) findViewById(R.id.viewFlipperCS);
		vflipperFA = (ViewFlipper) findViewById(R.id.viewFlipperFA);

		// registerForContextMenu(getListView());

		// *** Carga Spinner Filtro - Medicos ***//
		Spinner spinficme = (Spinner) findViewById(R.id.spinficherome);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.filtroficheroME,
				android.R.layout.simple_spinner_item); // Creamos el adaptador
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// Añadimos
																						// el
																						// layout
																						// para
																						// el
																						// menú
		spinficme.setAdapter(adapter); // Le indicamos al spinner el adaptador a
										// usar

		// *** Carga Spinner Filtro - CS ***//
		Spinner spinficcs = (Spinner) findViewById(R.id.spinficherocs);
		ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
				this, R.array.filtroficheroCS,
				android.R.layout.simple_spinner_item);
		adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinficcs.setAdapter(adapter1);

		// *** Carga Spinner Filtro - Farmacias ***//
		Spinner prueba11 = (Spinner) findViewById(R.id.spinficherofa);
		ArrayAdapter<CharSequence> adapter11 = ArrayAdapter.createFromResource(
				this, R.array.filtroficheroFA,
				android.R.layout.simple_spinner_item);
		adapter11
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		prueba11.setAdapter(adapter11);

		/* cargar pestañas */
		Resources res = getResources();

		// TabHost tabs=(TabHost)findViewById(android.R.id.tabhost);
		tabs = (TabHost) findViewById(android.R.id.tabhost);
		tabs.setup();

		TabHost.TabSpec spec = tabs.newTabSpec("mitab1");
		spec.setContent(R.id.tab1);
		spec.setIndicator("", res.getDrawable(R.drawable.iconome));
		tabs.addTab(spec);

		spec = tabs.newTabSpec("mitab2");
		spec.setContent(R.id.tab2);
		spec.setIndicator("", res.getDrawable(R.drawable.iconocs));
		tabs.addTab(spec);

		spec = tabs.newTabSpec("mitab3");
		spec.setContent(R.id.tab3);
		spec.setIndicator("", res.getDrawable(R.drawable.iconofa));
		tabs.addTab(spec);

		tabs.setCurrentTab(0);

		tabs.setOnTabChangedListener(new OnTabChangeListener() {
			// @Override
			public void onTabChanged(String tabId) {

				// nota: crear validacion que dependiendo de la tab carguen los
				// listviews
				// correspondientes para evitar retardo a la carga del
				// formulario

				if (vflipperME.getDisplayedChild() == 1) {
					/*
					 * if (cambiodatos()==True){ GuardarCambios(); }
					 */
					// GuardarCambios();
				}

				vflipperME.setDisplayedChild(0);
				vflipperCS.setDisplayedChild(0);
				vflipperFA.setDisplayedChild(0);

				if (tabs.getCurrentTab() == 1) {

					final ListView lvficCS = (ListView) findViewById(R.id.listficherocs);
					lvficCS.setAdapter(new miAdapterlvficCS(context));
					// listview de fichero de centros de salud
					final ListView lvsolCS = (ListView) findViewById(R.id.listsolicitudcs);
					lvsolCS.setAdapter(new miAdapterlvsolCS(context));
					// listview de fichero de farmacias
					final ListView lvregCS = (ListView) findViewById(R.id.listregionalcs);
					lvregCS.setAdapter(new miAdapterlvregCS(context));

					// Menu contextual listview (QuickAction)
					// Items del menu contextual
					ActionItem viewItem = new ActionItem(ID_VIEW, "Ver",
							getResources().getDrawable(R.drawable.ic_view));
					ActionItem acceptItem = new ActionItem(ID_ACCEPT,
							"Modificar", getResources().getDrawable(
									R.drawable.ic_accept));
					ActionItem uploadItem = new ActionItem(ID_UPLOAD,
							"Eliminar", getResources().getDrawable(
									R.drawable.ic_up));
					ActionItem previsitItem = new ActionItem(ID_PREVISIT,
							"Pre-Visitar", getResources().getDrawable(
									R.drawable.ic_prev));

					final QuickAction mQuickAction = new QuickAction(context);
					// final QuickAction sQuickAction = new
					// QuickAction(context);
					// final QuickAction rQuickAction = new
					// QuickAction(context);

					mQuickAction.addActionItem(viewItem);
					mQuickAction.addActionItem(acceptItem);
					mQuickAction.addActionItem(uploadItem);
					mQuickAction.addActionItem(previsitItem);

					mQuickAction
							.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
								// @Override
								public void onItemClick(
										QuickAction quickAction, int pos,
										int actionId) {
									ActionItem actionItem = quickAction
											.getActionItem(pos);

									if (actionId == ID_ADD) { // Add item
																// selected
										Toast.makeText(
												getApplicationContext(),
												"Add item selected on row "
														+ mSelectedRow,
												Toast.LENGTH_SHORT).show();
									} else {
										Toast.makeText(
												getApplicationContext(),
												actionItem.getTitle()
														+ " item selected on row "
														+ mSelectedRow,
												Toast.LENGTH_SHORT).show();
									}
								}
							});

					lvficCS.setOnItemClickListener(new OnItemClickListener() {
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							mSelectedRow = position; // set the selected row

							mQuickAction.show(view);

							// change the right arrow icon to selected state
							mMoreIv = (ImageView) view
									.findViewById(R.id.i_more);
							mMoreIv.setImageResource(R.drawable.ic_list_more_selected);
						}
					});

					lvsolCS.setOnItemClickListener(new OnItemClickListener() {
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							mSelectedRow = position; // set the selected row

							mQuickAction.show(view);

							// change the right arrow icon to selected state
							mMoreIv = (ImageView) view
									.findViewById(R.id.i_more);
							mMoreIv.setImageResource(R.drawable.ic_list_more_selected);
						}
					});

					lvregCS.setOnItemClickListener(new OnItemClickListener() {
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							mSelectedRow = position; // set the selected row

							mQuickAction.show(view);

							// change the right arrow icon to selected state
							mMoreIv = (ImageView) view
									.findViewById(R.id.i_more);
							mMoreIv.setImageResource(R.drawable.ic_list_more_selected);
						}
					});

				} else if (tabs.getCurrentTab() == 2) {

					final ListView lvficFA = (ListView) findViewById(R.id.listficherofa);
					lvficFA.setAdapter(new miAdapterlvficFA(context));
					// listview de fichero de centros de salud
					final ListView lvsolFA = (ListView) findViewById(R.id.listsolicitudfa);
					lvsolFA.setAdapter(new miAdapterlvsolFA(context));
					// listview de fichero de farmacias
					final ListView lvregFA = (ListView) findViewById(R.id.listregionalfa);
					lvregFA.setAdapter(new miAdapterlvregFA(context));

					// Menu contextual listview (QuickAction)
					// Items del menu contextual
					ActionItem viewItem = new ActionItem(ID_VIEW, "Ver",
							getResources().getDrawable(R.drawable.ic_view));
					ActionItem acceptItem = new ActionItem(ID_ACCEPT,
							"Modificar", getResources().getDrawable(
									R.drawable.ic_accept));
					ActionItem uploadItem = new ActionItem(ID_UPLOAD,
							"Eliminar", getResources().getDrawable(
									R.drawable.ic_up));
					ActionItem previsitItem = new ActionItem(ID_PREVISIT,
							"Pre-Visitar", getResources().getDrawable(
									R.drawable.ic_prev));

					final QuickAction mQuickAction = new QuickAction(context);

					mQuickAction.addActionItem(viewItem);
					mQuickAction.addActionItem(acceptItem);
					mQuickAction.addActionItem(uploadItem);
					mQuickAction.addActionItem(previsitItem);

					lvficFA.setOnItemClickListener(new OnItemClickListener() {
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							mSelectedRow = position; // set the selected row

							mQuickAction.show(view);

							// change the right arrow icon to selected state
							mMoreIv = (ImageView) view
									.findViewById(R.id.i_more);
							mMoreIv.setImageResource(R.drawable.ic_list_more_selected);
						}
					});

					lvsolFA.setOnItemClickListener(new OnItemClickListener() {
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							mSelectedRow = position; // set the selected row

							mQuickAction.show(view);

							// change the right arrow icon to selected state
							mMoreIv = (ImageView) view
									.findViewById(R.id.i_more);
							mMoreIv.setImageResource(R.drawable.ic_list_more_selected);
						}
					});

					lvregFA.setOnItemClickListener(new OnItemClickListener() {
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							mSelectedRow = position; // set the selected row

							mQuickAction.show(view);

							// change the right arrow icon to selected state
							mMoreIv = (ImageView) view
									.findViewById(R.id.i_more);
							mMoreIv.setImageResource(R.drawable.ic_list_more_selected);
						}
					});
				}

			}
		});

		/* Instanciar listviews a cargar */
		/* Listview menu principal */
		final ListView l = (ListView) findViewById(R.id.listmenu);
		l.setAdapter(new miAdapter(this));

		// Listview de fichero medico
		final ListView lvficME = (ListView) findViewById(R.id.listficherome);
		lvficME.setAdapter(new miAdapterlvficMe(this));
		// listview de fichero de centros de salud
		final ListView lvsolME = (ListView) findViewById(R.id.listsolicitudme);
		lvsolME.setAdapter(new miAdapterlvsolMe(this));
		// listview de fichero de farmacias
		final ListView lvregME = (ListView) findViewById(R.id.listregionalme);
		lvregME.setAdapter(new miAdapterlvregMe(this));

		// Menu contextual listview (QuickAction)
		// Items del menu contextual
		ActionItem viewItem = new ActionItem(ID_VIEW, "Ver", getResources()
				.getDrawable(R.drawable.ic_view));
		ActionItem acceptItem = new ActionItem(ID_ACCEPT, "Modificar",
				getResources().getDrawable(R.drawable.ic_accept));
		ActionItem uploadItem = new ActionItem(ID_UPLOAD, "Eliminar",
				getResources().getDrawable(R.drawable.ic_up));
		ActionItem previsitItem = new ActionItem(ID_PREVISIT, "Pre-Visitar",
				getResources().getDrawable(R.drawable.ic_prev));

		final QuickAction mQuickAction = new QuickAction(context);

		mQuickAction.addActionItem(viewItem);
		mQuickAction.addActionItem(acceptItem);
		mQuickAction.addActionItem(uploadItem);
		mQuickAction.addActionItem(previsitItem);

		// setup the action item click listener
		mQuickAction
				.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
					// @Override
					public void onItemClick(QuickAction quickAction, int pos,
							int actionId) {
						ActionItem actionItem = quickAction.getActionItem(pos);

						if (actionId == ID_ADD) { // Add item selected
							Toast.makeText(getApplicationContext(),
									"Add item selected on row " + mSelectedRow,
									Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(
									getApplicationContext(),
									actionItem.getTitle()
											+ " item selected on row "
											+ mSelectedRow, Toast.LENGTH_SHORT)
									.show();
						}
					}
				});

		// setup on dismiss listener, set the icon back to normal
		mQuickAction.setOnDismissListener(new PopupWindow.OnDismissListener() {
			// @Override
			public void onDismiss() {
				mMoreIv.setImageResource(R.drawable.ic_list_more);
			}
		});

		// QuickAction para los distintintos listviews de fichero
		lvficME.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mSelectedRow = position; // set the selected row

				mQuickAction.show(view);

				// change the right arrow icon to selected state
				mMoreIv = (ImageView) view.findViewById(R.id.i_more);
				mMoreIv.setImageResource(R.drawable.ic_list_more_selected);
			}
		});

		lvsolME.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mSelectedRow = position; // set the selected row

				mQuickAction.show(view);

				// change the right arrow icon to selected state
				mMoreIv = (ImageView) view.findViewById(R.id.i_more);
				mMoreIv.setImageResource(R.drawable.ic_list_more_selected);
			}
		});

		/*
		 * lvregME.setOnItemClickListener(new OnItemClickListener() { public
		 * void onItemClick(AdapterView<?> parent, View view, int position, long
		 * id) { mSelectedRow = position; //set the selected row
		 * 
		 * mQuickAction.show(view);
		 * 
		 * //change the right arrow icon to selected state mMoreIv = (ImageView)
		 * view.findViewById(R.id.i_more);
		 * mMoreIv.setImageResource(R.drawable.ic_list_more_selected); } });
		 */

		/* Eventos a realizar al sellecionar algun item del menu principal */
		l.setOnItemClickListener(new OnItemClickListener() {

			// @Override
			public void onItemClick(AdapterView<?> parent, View v,
					int posicion, long id) {

				switch (posicion) {
				case 0:
					/*
					 * Intent intentfic = new Intent(frmficheros.this,
					 * frmficheros.class); startActivity(intentfic);
					 */
					break;
				case 1:
					Intent intentprev = new Intent(frmficheros.this,
							frmprevisitas.class);
					startActivity(intentprev);
					break;
				case 2:
					Intent intentpost = new Intent(frmficheros.this,
							frmpostvisitas.class);
					startActivity(intentpost);
					break;
				case 3:
					Intent intentnotif = new Intent(frmficheros.this,
							frmnotificaciones.class);
					startActivity(intentnotif);
					break;
				case 4:
					Intent intentrep = new Intent(frmficheros.this,
							frmreportes.class);
					startActivity(intentrep);
					break;
				case 5:
					Intent intentaus = new Intent(frmficheros.this,
							frmausencias.class);
					startActivity(intentaus);
					break;
				case 6:
					Intent intentsinc = new Intent(frmficheros.this,
							frmsincronizar.class);
					startActivity(intentsinc);
					break;
				case 7:
					Intent intentconf = new Intent(frmficheros.this,
							frmconfiguracion.class);
					startActivity(intentconf);
					break;
				case 8:
					/*
					 * Intent intentlogin = new Intent(frmficheros.this,
					 * LoginActivity.class); startActivity(intentlogin);
					 * finishFromChild(getParent()); moveTaskToBack(true);
					 */
					ExitApp();
					break;
				}
			}
		});
	}

	/* Referencia a base de datos - No funciona */
	public void DisplayContact(Cursor c) {
		Toast.makeText(
				this,
				// "id: " + c.getString(0) + "\n" + "Usuario: " + c.getString(1)
				// + "\n" + "Clave: " + c.getString(2), Toast.LENGTH_LONG)
				"Centro: " + c.getString(0) + "\n" + "Ciudad: "
						+ c.getString(1), Toast.LENGTH_LONG).show();
	}

	/* cargar item del listview de menu principal */
	private static class miAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		private static final String[][] data = { { "Ficheros", "1" },
				{ "PreVisitas", "2" }, { "PostVisitas", "3" },
				{ "Notificaciones", "4" }, { "Reportes", "5" },
				{ "Ausencias", "6" }, { "Sincronizar", "7" },
				{ "Configuracion", "8" }, { "Salir", "9" } };

		private static final int[][] dataimg = { { R.drawable.icono, 1 },
				{ R.drawable.icono1, 2 }, { R.drawable.icono2, 3 },
				{ R.drawable.icono3, 4 }, { R.drawable.icono4, 5 },
				{ R.drawable.icono5, 6 }, { R.drawable.icono6, 7 },
				{ R.drawable.icono7, 8 }, { R.drawable.icono8, 9 } };

		public miAdapter(Context context) {

			mInflater = LayoutInflater.from(context);

		}

		public View getView(int position, View convertView, ViewGroup parent) {

			TextView text;

			ImageView img1;

			if (convertView == null) {

				convertView = mInflater.inflate(R.layout.listview_fila, null);

			}

			text = (TextView) convertView.findViewById(R.id.TextView01);

			img1 = (ImageView) convertView.findViewById(R.id.ImageView01);

			text.setText(data[position][0]);

			img1.setImageResource(dataimg[position][0]);

			// cambia estilo registro seleccionado en menu
			if (data[position][0] == "Ficheros") {
				convertView.setSelected(true);
				convertView.setBackgroundResource(R.drawable.degrade_botonmenu);
			} else {
				convertView.setSelected(true);
				convertView
						.setBackgroundResource(R.drawable.degrade_transparent);
			}

			return convertView;
		}

		public int getCount() {
			return data.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

	}

	/* cargar item de listview de fichero de medicos */
	private static class miAdapterlvficMe extends BaseAdapter {

		private LayoutInflater mInflater;

		private static final String[][] data = {
				{ "Alvares, ", "Tibisay", "Especialidad: MEG", "Frecuencia: 1",
						"Visita: 0", "1" },
				{ "Alvares, ", "Tibisay", "Especialidad: MEG", "Frecuencia: 1",
						"Visita: 0", "2" },
				{ "Alvares, ", "Tibisay", "Especialidad: MEG", "Frecuencia: 1",
						"Visita: 0", "3" } };

		public miAdapterlvficMe(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			TextView text;
			TextView text1;
			TextView text2;
			TextView text3;
			TextView text4;

			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.listview_fila_ficherome, null);
			}

			text = (TextView) convertView.findViewById(R.id.TextView01);
			text1 = (TextView) convertView.findViewById(R.id.TextView02);
			text2 = (TextView) convertView.findViewById(R.id.TextView03);
			text3 = (TextView) convertView.findViewById(R.id.TextView04);
			text4 = (TextView) convertView.findViewById(R.id.TextView05);

			text.setText(data[position][0]);
			text1.setText(data[position][1]);
			text2.setText(data[position][2]);
			text3.setText(data[position][3]);
			text4.setText(data[position][4]);

			return convertView;
		}

		public int getCount() {
			return data.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

	}

	/* cargar item de listview de solicitudes de medico */
	private static class miAdapterlvsolMe extends BaseAdapter {

		private LayoutInflater mInflater;

		private static final String[][] data = {
				{ "Alvares, ", "Tibisay", "Especialidad: MEG", "Frecuencia: 1",
						"Visita: 0", "1" },
				{ "Alvares, ", "Tibisay", "Especialidad: MEG", "Frecuencia: 1",
						"Visita: 0", "2" } };

		public miAdapterlvsolMe(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			TextView text;
			TextView text1;
			TextView text2;
			TextView text3;
			TextView text4;

			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.listview_fila_ficherome, null);
			}

			text = (TextView) convertView.findViewById(R.id.TextView01);
			text1 = (TextView) convertView.findViewById(R.id.TextView02);
			text2 = (TextView) convertView.findViewById(R.id.TextView03);
			text3 = (TextView) convertView.findViewById(R.id.TextView04);
			text4 = (TextView) convertView.findViewById(R.id.TextView05);

			text.setText(data[position][0]);
			text1.setText(data[position][1]);
			text2.setText(data[position][2]);
			text3.setText(data[position][3]);
			text4.setText(data[position][4]);

			return convertView;
		}

		public int getCount() {
			return data.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

	}

	/* cargar item de listview de fichero regional de medicos */
	private static class miAdapterlvregMe extends BaseAdapter {

		private LayoutInflater mInflater;

		private static final String[][] data = { { "Alvares, ", "Tibisay",
				"Especialidad: MEG", "Frecuencia: 1", "Visita: 0", "1" } };

		public miAdapterlvregMe(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			TextView text;
			TextView text1;
			TextView text2;
			TextView text3;
			TextView text4;

			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.listview_fila_ficherome, null);
			}

			text = (TextView) convertView.findViewById(R.id.TextView01);
			text1 = (TextView) convertView.findViewById(R.id.TextView02);
			text2 = (TextView) convertView.findViewById(R.id.TextView03);
			text3 = (TextView) convertView.findViewById(R.id.TextView04);
			text4 = (TextView) convertView.findViewById(R.id.TextView05);

			text.setText(data[position][0]);
			text1.setText(data[position][1]);
			text2.setText(data[position][2]);
			text3.setText(data[position][3]);
			text4.setText(data[position][4]);

			return convertView;
		}

		public int getCount() {
			return data.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

	}

	private static class miAdapterlvficCS extends BaseAdapter {

		private LayoutInflater mInflater;

		private static final String[][] data = { { "Alvares, ", "Tibisay",
				"Especialidad: MEG", "Frecuencia: 1", "Visita: 0", "1" } };

		public miAdapterlvficCS(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			TextView text;
			TextView text1;
			TextView text2;
			TextView text3;
			TextView text4;

			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.listview_fila_ficherofa, null);
			}

			text = (TextView) convertView.findViewById(R.id.TextView01);
			text1 = (TextView) convertView.findViewById(R.id.TextView02);
			text2 = (TextView) convertView.findViewById(R.id.TextView03);
			text3 = (TextView) convertView.findViewById(R.id.TextView04);
			text4 = (TextView) convertView.findViewById(R.id.TextView05);

			text.setText(data[position][0]);
			text1.setText(data[position][1]);
			text2.setText(data[position][2]);
			text3.setText(data[position][3]);
			text4.setText(data[position][4]);

			return convertView;
		}

		public int getCount() {
			return data.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

	}

	private static class miAdapterlvsolCS extends BaseAdapter {

		private LayoutInflater mInflater;

		private static final String[][] data = { { "Alvares, ", "Tibisay",
				"Especialidad: MEG", "Frecuencia: 1", "Visita: 0", "1" } };

		public miAdapterlvsolCS(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			TextView text;
			TextView text1;
			TextView text2;
			TextView text3;
			TextView text4;

			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.listview_fila_ficherofa, null);
			}

			text = (TextView) convertView.findViewById(R.id.TextView01);
			text1 = (TextView) convertView.findViewById(R.id.TextView02);
			text2 = (TextView) convertView.findViewById(R.id.TextView03);
			text3 = (TextView) convertView.findViewById(R.id.TextView04);
			text4 = (TextView) convertView.findViewById(R.id.TextView05);

			text.setText(data[position][0]);
			text1.setText(data[position][1]);
			text2.setText(data[position][2]);
			text3.setText(data[position][3]);
			text4.setText(data[position][4]);

			return convertView;
		}

		public int getCount() {
			return data.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

	}

	private static class miAdapterlvregCS extends BaseAdapter {

		private LayoutInflater mInflater;

		private static final String[][] data = { { "Alvares, ", "Tibisay",
				"Especialidad: MEG", "Frecuencia: 1", "Visita: 0", "1" } };

		public miAdapterlvregCS(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			TextView text;
			TextView text1;
			TextView text2;
			TextView text3;
			TextView text4;

			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.listview_fila_ficherofa, null);
			}

			text = (TextView) convertView.findViewById(R.id.TextView01);
			text1 = (TextView) convertView.findViewById(R.id.TextView02);
			text2 = (TextView) convertView.findViewById(R.id.TextView03);
			text3 = (TextView) convertView.findViewById(R.id.TextView04);
			text4 = (TextView) convertView.findViewById(R.id.TextView05);

			text.setText(data[position][0]);
			text1.setText(data[position][1]);
			text2.setText(data[position][2]);
			text3.setText(data[position][3]);
			text4.setText(data[position][4]);

			return convertView;
		}

		public int getCount() {
			return data.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

	}

	private static class miAdapterlvficFA extends BaseAdapter {

		private LayoutInflater mInflater;

		private static final String[][] data = { { "Alvares, ", "Tibisay",
				"Especialidad: MEG", "Frecuencia: 1", "Visita: 0", "1" } };

		public miAdapterlvficFA(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			TextView text;
			TextView text1;
			TextView text2;
			TextView text3;
			TextView text4;

			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.listview_fila_ficherofa, null);
			}

			text = (TextView) convertView.findViewById(R.id.TextView01);
			text1 = (TextView) convertView.findViewById(R.id.TextView02);
			text2 = (TextView) convertView.findViewById(R.id.TextView03);
			text3 = (TextView) convertView.findViewById(R.id.TextView04);
			text4 = (TextView) convertView.findViewById(R.id.TextView05);

			text.setText(data[position][0]);
			text1.setText(data[position][1]);
			text2.setText(data[position][2]);
			text3.setText(data[position][3]);
			text4.setText(data[position][4]);

			return convertView;
		}

		public int getCount() {
			return data.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

	}

	private static class miAdapterlvsolFA extends BaseAdapter {

		private LayoutInflater mInflater;

		private static final String[][] data = { { "Alvares, ", "Tibisay",
				"Especialidad: MEG", "Frecuencia: 1", "Visita: 0", "1" } };

		public miAdapterlvsolFA(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			TextView text;
			TextView text1;
			TextView text2;
			TextView text3;
			TextView text4;

			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.listview_fila_ficherofa, null);
			}

			text = (TextView) convertView.findViewById(R.id.TextView01);
			text1 = (TextView) convertView.findViewById(R.id.TextView02);
			text2 = (TextView) convertView.findViewById(R.id.TextView03);
			text3 = (TextView) convertView.findViewById(R.id.TextView04);
			text4 = (TextView) convertView.findViewById(R.id.TextView05);

			text.setText(data[position][0]);
			text1.setText(data[position][1]);
			text2.setText(data[position][2]);
			text3.setText(data[position][3]);
			text4.setText(data[position][4]);

			return convertView;
		}

		public int getCount() {
			return data.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

	}

	private static class miAdapterlvregFA extends BaseAdapter {

		private LayoutInflater mInflater;

		private static final String[][] data = { { "Alvares, ", "Tibisay",
				"Especialidad: MEG", "Frecuencia: 1", "Visita: 0", "1" } };

		public miAdapterlvregFA(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			TextView text;
			TextView text1;
			TextView text2;
			TextView text3;
			TextView text4;

			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.listview_fila_ficherofa, null);
			}

			text = (TextView) convertView.findViewById(R.id.TextView01);
			text1 = (TextView) convertView.findViewById(R.id.TextView02);
			text2 = (TextView) convertView.findViewById(R.id.TextView03);
			text3 = (TextView) convertView.findViewById(R.id.TextView04);
			text4 = (TextView) convertView.findViewById(R.id.TextView05);

			text.setText(data[position][0]);
			text1.setText(data[position][1]);
			text2.setText(data[position][2]);
			text3.setText(data[position][3]);
			text4.setText(data[position][4]);

			return convertView;
		}

		public int getCount() {
			return data.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

	}

}
